package com.example.photography.Activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.photography.R;
import com.example.photography.adapter.HotproductAdapter;
import com.example.photography.bean.Product_bean;
import com.example.photography.common.BaseActivity;
import com.example.photography.util.Util;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 热门榜单
 */
public class HotProduct_List extends BaseActivity {


    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.rl_title)
    RelativeLayout rlTitle;
    @BindView(R.id.product_list)
    ListView productList;
    private List<Product_bean> productBeans;
    private HotproductAdapter hotproductAdapter;
    @Override
    protected void initData() {
        String content = Util.getJson(this, "hot_list.json");
        if (TextUtils.isEmpty(content)) {//数据为空
            return;
        }
        JSONObject jsonObject = JSON.parseObject(content);
        int status = jsonObject.getInteger("code");
        if (status == 202) {
            String data = jsonObject.getString("data");
            String data1 = JSON.parseObject(data).getString("hot_product");
            //封装转json数据
            productBeans = JSONObject.parseArray(data1, Product_bean.class);
            initView();
        } else {
            // TODO: 2021/5/2 加没数据的判断
        }
        initListen();
    }

    private void initView() {
        tvTitle.setText("- 每周榜单-");
        hotproductAdapter = new HotproductAdapter(productBeans,this);
        productList.setAdapter(hotproductAdapter);
    }

    private void initListen() {
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                removeCurrentActivity();
            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_hotproduct_list;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}